<?php

namespace addons\zzarticle\model;

use think\Model;
use think\model\relation\BelongsTo;

class ArticleM extends Model
{
    const STATUS_NORMAL = 'normal';
    protected $table = 'zzarticle_article';

    protected $autoWriteTimestamp = 'int';
    protected $createTime         = 'create_at';

    protected $append = [
        'images_array',
        'create_at',
    ];

    public function getImagesArrayAttr($value, $data): array
    {
        $r = $data['images'] ?? '';
        if (empty($r)) {
            return [];
        }
        return array_map(function ($item) {
            return cdnurl($item);
        }, explode(',', $r));
    }

    public function getCreateAtAttr($value, $data): string
    {
        $r = $data['create_at'] ?? 0;
        if (empty($r)) {
            return '';
        }
        return date('Y-m-d H:i:s', $r);
    }

    public function type(): BelongsTo
    {
        return $this->belongsTo(ArticleTypeM::class, 'type_id', 'id')->setEagerlyType(0);
    }
}
